package game.entities;

import game.InputHandler;
import game.gfx.Colors;
import game.level.Level;
import game.level.tiles.PortalTile;
import game.level.tiles.Tile;

public abstract class Player extends Mob {

	protected InputHandler input;
	protected int color = Colors.get(-1, 111, 145, 543); // (-1, 111, 333, 543)
															// for sword
	protected int swordColor = Colors.get(-1, 111, 333, 555);
	protected int scale = 1;
	protected boolean isSwimming = false;
	protected int tickCount = 0;
	protected String username;
	protected int classType;
	protected static boolean heldJump = false;
	protected long timeNow = System.currentTimeMillis();
	protected long lastSwing = System.currentTimeMillis();
	protected boolean heldSwing = false;
	protected boolean hasSwung = false;
	protected long begSwing = System.currentTimeMillis();
	protected long timeAftBegSwing = System.currentTimeMillis();
	protected boolean shouldIterate = true;

	public Player(Level level, int x, int y, InputHandler input,
			String username, int fallSpdForDmg, int maxSpeed) { // need
		// to
		// add
		// a
		// health
		// attribute
		// here.
		super(level, "Player", x, y, 1, maxSpeed);
		this.input = input;
		this.username = username;
		this.health = 200;
		this.maxHealth = 200;
		this.classType = 0;
		this.fallSpdForDmg = fallSpdForDmg;
		this.isEnemy = false;
	}

	public void tick() {
		int xa = 0;
		int ya = 0;
		if (input != null) {
			if (input.up.isPressed()) {
				ya--;
				heldJump = true;
			} else {
				heldJump = false;
			}
			if (input.down.isPressed()) {
				ya++;
			}
			if (input.left.isPressed()) {
				xa--;
			}
			if (input.right.isPressed()) {
				xa++;
			}
		}
		// if(xa != 0 || ya != 0){
		move(xa, ya);
		isMoving = true;

		// } else {
		isMoving = false;
		// }
		if (!isMoving) {
			// y+=1;
		}
		if (level.getTile(this.x / 8, this.y / 8).getId() == 3) {
			isSwimming = true;
		}
		if (isSwimming && level.getTile(this.x / 8, this.y / 8).getId() != 3) {
			isSwimming = false;
		}
		Tile lastTile = level.getTile((this.x + 7) >> 3, (this.y + 7) >> 3);
		if ((isSolidTile(1, 0, 5, 7) || isSolidTile(1, 0, 6, 7)
				|| isSolidTile(-1, 0, 2, 7) || isSolidTile(-1, 0, 1, 7) || lastTile
					.isSolid()) && !isSolidTile(0, 1, 5, 7)) {
			if (!isSolidTile(-1, 0, 1, 3) && !isSolidTile(1, 0, 6, 3)) {
				y--;
			} else if (isSolidTile(1, 0, 5, 7) || isSolidTile(1, 0, 6, 7)) {
				x--;
			} else {
				x++;
			}
		}
		tickCount++;
	}

	public boolean hasCollided(int xa, int ya) {
		int xMin = 0;
		int xMax = 7;
		int yMin = -7;
		int yMax = 7;
		for (int x = xMin; x < xMax; x++) {
			if (isSolidTile(xa, ya, x, yMin)) {
				return true;
			}
			if (isPortalTile(xa, ya, x, yMin)) {
				PortalTile targ = (PortalTile) level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3);
		        targ.goToDestination(this);
			}
		}
		for (int x = xMin; x < xMax; x++) {
			if (isSolidTile(xa, ya, x, yMax)) {
				return true;
			}
			if (isPortalTile(xa, ya, x, yMax)) {
				PortalTile targ = (PortalTile) level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3);
		        targ.goToDestination(this);
			}
		}
		for (int y = yMin; y < yMax; y++) {
			if (isSolidTile(xa, ya, xMin, y)) {
				return true;
			}
			if (isPortalTile(xa, ya, xMin, y)) {
				PortalTile targ = (PortalTile) level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3);
		        targ.goToDestination(this);
			}
		}
		for (int y = yMin; y < yMax; y++) {
			if (isSolidTile(xa, ya, xMax, y)) {
				return true;
			}
			if (isPortalTile(xa, ya, xMax, y)) {
				PortalTile targ = (PortalTile) level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3);
		        targ.goToDestination(this);
			}
		}
		return false;
	}

	public String getUsername() {
		return this.username;
	}

	public static boolean getHeldJump() {
		return heldJump;
	}

	protected boolean isPortalTile(int xa, int ya, int x, int y) {
		if (level == null)
			return false;
		Tile lastTile = level.getTile((this.x + x) >> 3, (this.y + y) >> 3);
		Tile newTile = level.getTile((this.x + x + xa) >> 3,
				(this.y + y + ya) >> 3);
		if (!lastTile.equals(newTile) && newTile.isPortal()) {
			return true;
		}
		return false;
	}

	public void teleport(int destX, int destY) {
		this.x = destX;
		this.y = destY;

	}

}

